package com.movitech.mobile.cz.base.sys.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.sys.bizvo.DictRequestVO;
import com.movitech.mobile.cz.base.sys.entity.SysDict;
import com.movitech.mobile.cz.base.sys.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * SysDict 前端控制器
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@RestController
@RequestMapping("/v1/sysDict")
@Api(value = "基础操作接口", description = "基础操作接口")
public class SysDictController {
    @Autowired
    @Qualifier("sysDictService")
    private SysDictService sysDictService;

    @GetMapping("/allList")
    @ApiOperation(
            value = "获取所有接口",
            notes = "获取所有接口",
            response = SysDict.class
    )
    public RestReturnResult list() {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SysDict> page = sysDictService.selectPage(new Page<SysDict>());
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @PostMapping("/searchList/{pageNumber}")
    @ApiOperation(
            value = "列表查询接口",
            notes = "列表查询接口",
            response = SysDict.class
    )
    public RestReturnResult searchList(@RequestBody SysDict sysDict,
                                       @ApiParam(value = "分页页数") @PathVariable Integer pageNumber) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SysDict> page = new Page<>();
        page.setCurrent(pageNumber);
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.setEntity(sysDict);
        page = sysDictService.selectPage(page, wrapper);
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @GetMapping("/getSysDict/{id}")
    @ApiOperation(
            value = "对象ID获取接口",
            notes = "对对象ID获取接口",
            response = SysDict.class
    )
    public RestReturnResult addSysDict(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        SysDict sysDict = sysDictService.selectById(id);
        restReturnResult.setData(sysDict);
        return restReturnResult;
    }

    @GetMapping("/getSysDictByCode/{code}")
    @ApiOperation(value = "供应商各个列表状态接口", notes = "供应商各个列表状态接口")
    public RestReturnResult getSysDictByCode(@PathVariable String code) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        List<SysDict> sysDicts = sysDictService.getSysDictByCode(code);
        restReturnResult.setData(sysDicts);
        return restReturnResult;
    }

    @PostMapping("/add")
    @ApiOperation(
            value = "添加接口",
            notes = "添加接口",
            response = SysDict.class
    )
    public RestReturnResult add(@RequestBody SysDict sysDict) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sysDictService.insert(sysDict);
        restReturnResult.setData(sysDict);
        return restReturnResult;
    }

    @PutMapping("/update")
    @ApiOperation(
            value = "更新接口",
            notes = "更新接口",
            response = SysDict.class
    )
    public RestReturnResult update(@RequestBody SysDict sysDict) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sysDictService.updateById(sysDict);
        restReturnResult.setData(sysDict);
        return restReturnResult;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(
            value = "通过ID删除接口",
            notes = "通过ID删除接口",
            response = SysDict.class
    )
    public RestReturnResult update(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sysDictService.deleteById(id);
        return restReturnResult;
    }

    @ApiOperation(value = "字典表查询", notes = "字典表查询")
    @RequestMapping(value = "/public/getDictListByDictType", method = RequestMethod.GET)
    @ResponseBody
    public RestReturnResult<Object> getDictListByDictType(String dictType,Integer resultType, String parentId){
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
        try {
            hResult = sysDictService.getDictListByDictType(dictType,resultType, parentId);
        } catch (Exception e) {
            e.printStackTrace();
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
        }
        return hResult;
    }

    /**
     * 字典表新增/修改接口
     *
     * @param
     * @return
     */
    @ApiOperation(
            value = "字典表新增/修改接口",
            notes = "字典表新增/修改接口",
            response = SysDict.class
    )
    @RequestMapping(value = "/saveOrUpdateDict", method = RequestMethod.POST)
    @ResponseBody
    public RestReturnResult<Object> saveOrUpdateDict(@RequestBody DictRequestVO reqVO) {
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);

        try {
            hResult = sysDictService.saveOrUpdateDict(reqVO);
        } catch (Exception e) {
            e.printStackTrace();
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
        }

        return hResult;
    }

    /**
     * 字典表删除接口
     *
     * @param id
     * @return
     */
    @ApiOperation(
            value = "字典表删除接口",
            notes = "字典表删除接口",
            response = SysDict.class
    )
    @RequestMapping(value = "/deleteDict", method = RequestMethod.GET)
    @ResponseBody
    public RestReturnResult<Object> deleteDict(String id) {
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);

        try {
            hResult = sysDictService.updateDictForDel(id);
        } catch (Exception e) {
            e.printStackTrace();
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
        }

        return hResult;
    }


    /**
     * 字典表查询获取Map格式
     *
     * @param type
     * @return
     */
    @ApiOperation(
            value = "字典表删除接口",
            notes = "字典表删除接口",
            response = SysDict.class
    )
    @RequestMapping(value = "/selectMap", method = RequestMethod.GET)
    @ResponseBody
    public RestReturnResult<Map<String,Object>> selectMap(@RequestParam String type) {
        RestReturnResult hResult =  RestReturnResult.success();
        hResult.setData( sysDictService.selectMapByType(type));
        return hResult;
    }


}

